<?php

/**
 * This is the model class for table "{{street}}".
 *
 * The followings are the available columns in table '{{street}}':
 * @property integer $id
 * @property string $streetname
 * @property string $sheng
 * @property string $shi
 * @property string $qu
 * @property string $fzr
 * @property string $lxdh
 * @property string $bgdh
 * @property string $dz
 * @property string $bz
 */
class Street extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{street}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('streetname, sheng, shi, qu', 'required'),
			array('streetname, sheng, shi, qu', 'length', 'max'=>50),
			array('fzr, lxdh, bgdh', 'length', 'max'=>20),
			array('dz, bz', 'length', 'max'=>100),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
            array('sheng','default','value'=>'辽宁省'),
            array('shi','default','value'=>'沈阳市'),
            array('qu','default','value'=>YII::app()->user->uuid),
			array('id, streetname, sheng, shi, qu, fzr, lxdh, bgdh, dz, bz', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
 	      return array(
            'sqs'=>array(self::HAS_MANY,'Sq','streetid'),
		);
	}

    public function dataobject() {
         $sline = '{"沈河区": {';
        $streetlist = Street::model()->findAll(); 
         foreach($streetlist as $item){
            $sline .= '"'.$item->streetname . '":[';
            foreach($item->sqs as $one) {
                $sline .= '"'. $one->sqname .'",';
            }
                $sline .= '],';
        }
        $sline .= '}}';
       return CJavaScript::jsonEncode($sline);
    }
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
		'id' => '序号',
			'streetname' => '街道',
			'sheng' => '省',
			'shi' => '市',
			'qu' => '区',
			'fzr' => '负责人',
			'lxdh' => '联系电话',
			'bgdh' => '办公电话',
			'dz' => '地址',
			'bz' => '备注',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('streetname',$this->streetname,true);
		$criteria->compare('sheng',$this->sheng,true);
		$criteria->compare('shi',$this->shi,true);
		$criteria->compare('qu',$this->qu,true);
		$criteria->compare('fzr',$this->fzr,true);
		$criteria->compare('lxdh',$this->lxdh,true);
		$criteria->compare('bgdh',$this->bgdh,true);
		$criteria->compare('dz',$this->dz,true);
		$criteria->compare('bz',$this->bz,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Street the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
